package com.hmdp.config;

import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    //配置跨域
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        //token刷新拦截器, 拦截所有请求，使用RefreshTokenInterceptor刷新token,该拦截器执行顺序为0,最先执行此拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate)).addPathPatterns("/**").order(0);
        // 登录拦截器，排除以下路径
        registry.addInterceptor(new LoginInterceptor())
                .excludePathPatterns(
                        "/shop/**",         // 排除对/shop/**路径的拦截
                        "/voucher/**",      // 排除对/voucher/**路径的拦截
                        "shop-type",        // 排除对/shop-type路径的拦截（可能是缺少斜杠："/shop-type"）
                        "/user/code",       // 排除对/user/code路径的拦截
                        "/upload/**",       // 排除对/upload/**路径的拦截
                        "/user/login",      // 排除对/user/login路径的拦截
                        "/blog/hot"         // 排除对/blog/hot路径的拦截
                ).order(1);             // 设置拦截器的执行顺序为1
    }
}
